idf_build_get_property(target IDF_TARGET)

set(target_folder "${target}")

# On Linux the soc component is a simple wrapper, without much functionality
if(NOT ${target} STREQUAL "linux")
    if(CONFIG_IDF_TARGET_ESP32H4 AND NOT CONFIG_ESP32H4_SELECTS_REV_MP) # TODO: ESP32H4 IDF-13835
        set(srcs "lldesc.c"
                "dport_access_common.c"
                "${target_folder}/interrupts_beta5.c"
                "${target_folder}/gpio_periph.c"
                "${target_folder}/uart_periph.c")
    else()
        set(srcs "lldesc.c"
                "dport_access_common.c"
                "${target_folder}/interrupts.c"
                "${target_folder}/gpio_periph.c"
                "${target_folder}/uart_periph.c")
    endif()
endif()

set(includes "include" "${target_folder}")

if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${target_folder}/include")
    # miscellaneous headers, like definitions, man-made register headers, wrappers, etc.
    list(APPEND includes "${target_folder}/include")
    if(CONFIG_IDF_TARGET_ESP32H4) # TODO: ESP32H4 IDF-13835
        if(CONFIG_ESP32H4_SELECTS_REV_MP)
            list(APPEND includes "${target_folder}/include/hw_ver_mp")
        else()
            list(APPEND includes "${target_folder}/include/hw_ver_beta5")
        endif()
    endif()
endif()

# register headers that generated by script from CSV
if(CONFIG_IDF_TARGET_ESP32P4)
    if(CONFIG_ESP32P4_SELECTS_REV_LESS_V3)
        list(APPEND includes "${target_folder}/register/hw_ver1")
    else()
        list(APPEND includes "${target_folder}/register/hw_ver3")
    endif()
elseif(CONFIG_IDF_TARGET_ESP32H21) # TODO: ESP32H21 IDF-13923
    list(APPEND includes "${target_folder}/register")
    if(CONFIG_ESP32H21_SELECTS_REV_MP)
        list(APPEND includes "${target_folder}/register/hw_ver_mp")
    else()
        list(APPEND includes "${target_folder}/register/hw_ver_beta1")
    endif()
elseif(CONFIG_IDF_TARGET_ESP32H4) # TODO: ESP32H4 IDF-13835
    list(APPEND includes "${target_folder}/register")
    if(CONFIG_ESP32H4_SELECTS_REV_MP)
        list(APPEND includes "${target_folder}/register/hw_ver_mp")
    else()
        list(APPEND includes "${target_folder}/register/hw_ver_beta5")
    endif()
else()
    if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${target_folder}/register")
        list(APPEND includes "${target_folder}/register")
    endif()
endif()

if(target STREQUAL "esp32")
    list(APPEND srcs "${target_folder}/dport_access.c")
endif()

if(CONFIG_SOC_ADC_SUPPORTED)
    list(APPEND srcs "${target_folder}/adc_periph.c")
endif()

if(CONFIG_SOC_ANA_CMPR_SUPPORTED)
    list(APPEND srcs "${target_folder}/ana_cmpr_periph.c")
endif()

if(CONFIG_SOC_DEBUG_PROBE_SUPPORTED)
    list(APPEND srcs "${target_folder}/debug_probe_periph.c")
endif()


if(CONFIG_SOC_GPSPI_SUPPORTED)
    list(APPEND srcs "${target_folder}/spi_periph.c")
endif()

if(CONFIG_SOC_LEDC_SUPPORTED)
    list(APPEND srcs "${target_folder}/ledc_periph.c")
endif()

if(CONFIG_SOC_ISP_SUPPORTED)
    list(APPEND srcs "${target}/isp_periph.c")
endif()

if(CONFIG_SOC_I3C_MASTER_SUPPORTED)
    list(APPEND srcs "${target_folder}/i3c_master_periph.c")
endif()

if(CONFIG_SOC_TEMP_SENSOR_SUPPORTED)
    list(APPEND srcs "${target_folder}/temperature_sensor_periph.c")
endif()

if(CONFIG_SOC_MPI_SUPPORTED)
    list(APPEND srcs "${target_folder}/mpi_periph.c")
endif()

if(CONFIG_SOC_SDMMC_HOST_SUPPORTED)
    list(APPEND srcs "${target_folder}/sdmmc_periph.c")
endif()

if(CONFIG_SOC_TOUCH_SENSOR_SUPPORTED)
    list(APPEND srcs "${target_folder}/touch_sensor_periph.c")
endif()

if(CONFIG_SOC_IEEE802154_SUPPORTED)
    list(APPEND srcs "${target_folder}/ieee802154_periph.c")
endif()

if(CONFIG_SOC_DAC_SUPPORTED)
    list(APPEND srcs "${target_folder}/dac_periph.c")
endif()

if(CONFIG_SOC_SDIO_SLAVE_SUPPORTED)
    list(APPEND srcs "${target_folder}/sdio_slave_periph.c")
endif()

if(CONFIG_SOC_PAU_SUPPORTED AND CONFIG_SOC_LIGHT_SLEEP_SUPPORTED AND CONFIG_SOC_PM_SUPPORT_TOP_PD)
    list(APPEND srcs "${target_folder}/system_retention_periph.c")
endif()

if(CONFIG_SOC_BOD_SUPPORTED)
    list(APPEND srcs "${target_folder}/power_supply_periph.c")
endif()

idf_component_register(SRCS ${srcs}
                       INCLUDE_DIRS ${includes}
                       LDFRAGMENTS "linker.lf")

# For an embedded system, the MMU page size should always be defined statically
# For IDF, we define it according to the Flash size that user selects
# Replace this value in an adaptive way, if Kconfig isn't available on your platform
target_compile_definitions(${COMPONENT_LIB} INTERFACE SOC_MMU_PAGE_SIZE=CONFIG_MMU_PAGE_SIZE)

target_compile_definitions(${COMPONENT_LIB} INTERFACE SOC_XTAL_FREQ_MHZ=CONFIG_XTAL_FREQ)

if(target STREQUAL "esp32")
    # esp_dport_access_reg_read is added as an undefined symbol because otherwise
    # the linker can ignore dport_access.c as it would no other files depending on any symbols in it.
    set_property(TARGET ${COMPONENT_LIB} APPEND PROPERTY INTERFACE_LINK_LIBRARIES "-u esp_dport_access_reg_read")
endif()

if(NOT CONFIG_IDF_TARGET_LINUX)
    if(CONFIG_IDF_TARGET_ESP32H4 AND NOT CONFIG_ESP32H4_SELECTS_REV_MP) # TODO: ESP32H4 IDF-13835
        target_linker_script(${COMPONENT_LIB} INTERFACE "${target_folder}/ld/${target}.peripherals.beta5.ld")
    else()
        target_linker_script(${COMPONENT_LIB} INTERFACE "${target_folder}/ld/${target}.peripherals.ld")
    endif()
endif()
